﻿using System;
using System.Collections.Generic;
using System.Linq;
using QuanLyTiemThuocTay.cls;

namespace QuanLyTiemThuocTay.Report
{
    public partial class rptPhieuNhap : DevExpress.XtraReports.UI.XtraReport
    {
        QLTTDataContext db = new QLTTDataContext();
        List<CHITIETNHAPDVT> lctndvt = new List<CHITIETNHAPDVT>();
        private int STT;
        int tongsoluong = 0;
        int tongtien = 0;
        public rptPhieuNhap(string sophieu)
        {
            InitializeComponent();
            DisplayName = "Phiếu nhập";
            List<CHITIETNHAP> lctn = db.CHITIETNHAPs.Where(x => x.SoHDNhap == sophieu).ToList();
            HDNHAP hdn = db.HDNHAPs.SingleOrDefault(x => x.SoHDNhap == sophieu);
            lbSoPhieu.Text = sophieu;
            lbDaiDien.Text = hdn.DaiDien;
            var ncc = db.NHACUNGCAPs.SingleOrDefault(x => x.TenNhaCC == hdn.NhapTuDonVi);
            lbNgay.Text = DateTime.Parse(hdn.NgayNhap.ToString()).Day + "/" + DateTime.Parse(hdn.NgayNhap.ToString()).Month + "/" + DateTime.Parse(hdn.NgayNhap.ToString()).Year;
            lbNhaCC.Text = hdn.NhapTuDonVi;
            lbDiaChi.Text = db.NHACUNGCAPs.SingleOrDefault(x => x.TenNhaCC == hdn.NhapTuDonVi).DiaChi;
            if (hdn != null)
            {
                foreach (var item in lctn)
                {
                    CHITIETNHAPDVT te = new CHITIETNHAPDVT();
                    te.MaChiTiet = item.MaChiTiet;
                    te.MaDonGia = item.MaDonGia;
                    te.MaDVTinh = (int)db.THUOCs.Single(x => x.MaThuoc == item.MaThuoc).MaDVTinh;
                    te.MaThuoc = item.MaThuoc;
                    te.SoHDNhap = sophieu;
                    te.SoLuong = item.SoLuong;
                    lctndvt.Add(te);
                }
                try
                {
                    DataSource = lctndvt;
                }
                catch { }
                STT = 0;
            }

        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            STT++;
            xrTT.Text = STT.ToString();
            try { xrTen.Text = "  " + db.THUOCs.SingleOrDefault(x => x.MaThuoc == lctndvt[STT - 1].MaThuoc).TenThuoc; }
            catch { }
            try { xrDVT.Text = "  " + db.DVTINHs.SingleOrDefault(x => x.MaDVTinh == lctndvt[STT - 1].MaDVTinh).TenDVTinh; }
            catch { }
            try { xrDonGia.Text = db.GIATHUOCs.SingleOrDefault(x => x.MaDonGia == lctndvt[STT - 1].MaDonGia).GiaNhap + ""; }
            catch { }
            try
            {
                xrSoLuong.Text = lctndvt[STT - 1].SoLuong + "";
                tongsoluong += lctndvt[STT - 1].SoLuong;
            }
            catch { }
            try
            {
                xrThanhTien.Text = (lctndvt[STT - 1].SoLuong * Int32.Parse(xrDonGia.Text)) + "";
                tongtien += Int32.Parse(xrThanhTien.Text);
            }
            catch { }
            xrTongSLuong.Text = tongsoluong + "";
            xrTongTien.Text = tongtien + "";
            xrSoTienChu.Text = "Tổng tiền (bằng chữ): "+clsHam.DocTienBangChu(tongtien, " đồng y.");
        }

    }
}
